package Util::Topic::TopicCreate;
use Moose;
use namespace::autoclean;

has 'schema' => (
	is => 'rw',
	required => 1,
	isa => 'DBIx::Class::Schema',
);

sub create {
	my ($self, $biaoti, $neirong, $commented_id, $author_id, $topic_type_id, $danwei_id) = @_;
	my $store = $self->schema->resultset('Topic')->create({
			body => $neirong,
			title => $biaoti,
			comm_item_id => $commented_id,
			author_id => $author_id,
			danwei_id => $danwei_id,
			lupd_user_id => $author_id,
			created_by => $author_id,
			counter => 0,
			updated_by => $author_id,
			effective_to => '2999-01-01',
			topic_type_id => $topic_type_id
			});

}

sub update_counter {
        my ($self, $topic) = @_;
        my $counter = $topic->counter + 1;
        $topic->update({
                counter => $counter,
        });
}

sub update {
	my ($self,$topic_id, $biaoti, $neirong, $user_id, $topic_type_id, $danwei_id) = @_;
	my $topic = $self->schema->resultset('Topic')->find($topic_id);
	$topic->update({
		body => $neirong,
		title => $biaoti,
		updated_by => $user_id,
		topic_type_id => $topic_type_id,
		danwei_id => $danwei_id
	});
	return $topic;
}

sub change_jinghua{
	my ($self,$topic_id, $jinghua,$user_id) = @_;
	my $topic = $self->schema->resultset('Topic')->find($topic_id);
	$topic->update({
		is_jinghua => $jinghua,
		updated_by => $user_id
	});
	return $topic;
}

sub change_zhiding {
	my ($self,$topic_id, $zhiding, $user_id) = @_;
	my $topic = $self->schema->resultset('Topic')->find($topic_id);
	$topic->update({
		is_zhiding => $zhiding,
		updated_by => $user_id
	});
	return $topic;
}

sub expire_topic {
	my ($self,$topic_id) = @_;
	my $topic = $self->schema->resultset('Topic')->find($topic_id);
	$topic->update({
		#effective_to => localtime
		effective_to => `date --iso-8601=seconds` 
	});
	return $topic;
}

__PACKAGE__->meta->make_immutable;

1;
